package org.karelun.whiteboard.edit.properties;

import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.karelun.whiteboard.Location;
import org.karelun.whiteboard.WhiteboardPackage;

/**
 * @Author: KarelunHuang
 * @Date: Jan 30, 2013
 **/
public class LocationPropertySection extends BasePropertySection<Location> {

	private Text xText;
	
	private Text yText;
	
	private ISWTObservableValue xTextObserver;

	private ISWTObservableValue yTextObserver;
	
	public LocationPropertySection() {
	}

	@Override
	protected String getSectionTitle() {
		return "Location";
	}

	@Override
	protected void doCreateControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(4, false));
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		new Label(composite, SWT.NONE).setText("X:");
		xText=new Text(composite, SWT.BORDER);
		xText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		xTextObserver=WidgetProperties.text(SWT.Modify).observe(xText);
		
		new Label(composite, SWT.NONE).setText("Y:");
		yText=new Text(composite, SWT.BORDER);
		yText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		yTextObserver=WidgetProperties.text(SWT.Modify).observe(yText);
	}

	@Override
	protected void dataBinding() {
		Location location = getSelectedObjectModel();
		dataBinding(xTextObserver, location, WhiteboardPackage.Literals.LOCATION__X,Integer.class);
		dataBinding(yTextObserver, location, WhiteboardPackage.Literals.LOCATION__Y,Integer.class);
	}
	
	@Override
	public void refresh() {
		if(xText!=null&&!xText.isDisposed()){
			xText.setEnabled(isEditable());
		}
		if(yText!=null&&!yText.isDisposed()){
			yText.setEnabled(isEditable());
		}
	}
}
